package home.my.base.sql;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

public class ConditionQuery{
	private String pojoName;
	private String orders;
//	private int pageNo = 1;
//	private int pageSize = 0;
	private List<AbsCondition> conditionList = new ArrayList<AbsCondition>();
	
	private boolean inited = false;
	private String hql = "";
	private List<Serializable> valueList = new ArrayList<Serializable>();
	
	public ConditionQuery(String pojoName){
		this.pojoName = pojoName;
	}
	
	public ConditionQuery(String pojoName,String orders){
		this.pojoName = pojoName;
		this.orders = orders;
	}
	
	public ConditionQuery(String pojoName,String orders,List<AbsCondition> conditionList){
		this.pojoName = pojoName;
		this.conditionList = conditionList;
	}
	
	public ConditionQuery(String pojoName,String orders,AbsCondition... conds){
		this.pojoName = pojoName;
		if(conds!=null && conds.length>0){
			for(AbsCondition c:conds){
				add(c);
			}
		}
	}
	
	public ConditionQuery(Class<?> t){
		this.pojoName = t.getSimpleName();
	}
	
	public ConditionQuery(Class<?> t,String orders){
		this.pojoName =  t.getSimpleName();
		this.orders = orders;
	}
	
	public ConditionQuery(Class<?> t,String orders,List<AbsCondition> conditionList){
		this.pojoName =  t.getSimpleName();
		this.conditionList = conditionList;
	}
	
	public ConditionQuery(Class<?> t,String orders,AbsCondition... conds){
		this.pojoName =  t.getSimpleName();
		if(conds!=null && conds.length>0){
			for(AbsCondition c:conds){
				add(c);
			}
		}
	}
	
	public void add(AbsCondition cond){
		this.inited = false;
		this.conditionList.add(cond);
	}
	
	public String getHql() {
		init();
		return hql;
	}
	public Serializable[] getValueArray() {
		init();
		return valueList.toArray(new Serializable[0]);
	}
	
	private void init() {
		if(inited){
			return;
		}
		StringBuilder hqlBuilder = new StringBuilder(" from ");
		hqlBuilder.append(pojoName);
		hqlBuilder.append(" t where 1=1 ");
		if(conditionList!=null && conditionList.size()>0){
			for(AbsCondition cond:conditionList){
				String chql = cond.toSql();
				Serializable val = cond.getValue();
				if(StringUtils.isNotBlank(chql)){
					hqlBuilder.append(" and ");
					hqlBuilder.append(chql);
					if(val!=null){
						valueList.add(val);
					}
				}
			}
		}
		hqlBuilder.append(StringUtils.isNotEmpty(orders)?" order by "+orders:"");
		this.inited = true;
		this.hql = hqlBuilder.toString();
	}
	public String getPojoName() {
		return pojoName;
	}
	public void setPojoName(String pojoName) {
		this.inited = false;
		this.pojoName = pojoName;
	}
	public String getOrders() {
		return orders;
	}
	public void setOrders(String orders) {
		this.inited = false;
		this.orders = orders;
	}
//	public int getPageNo() {
//		return pageNo;
//	}
//	public void setPageNo(int pageNo) {
//		this.inited = false;
//		this.pageNo = pageNo;
//	}
//	public int getPageSize() {
//		return pageSize;
//	}
//	public void setPageSize(int pageSize) {
//		this.inited = false;
//		this.pageSize = pageSize;
//	}
	public List<AbsCondition> getConditionList() {
		return conditionList;
	}
	public void setConditionList(List<AbsCondition> conditionList) {
		this.conditionList = conditionList;
	}
	
}
